﻿Public Class frmPhieuMuon
    Dim maphieuhople As Boolean
    Dim check As String
    Dim t As Integer = 0
    Dim NgayLap As Date
    Dim dateUSFormat As System.IFormatProvider = New System.Globalization.CultureInfo("en-US", True)
    Dim dateVNFormat As System.IFormatProvider = New System.Globalization.CultureInfo("vi-VN", True)
    Dim pmct As New DataTable
    Dim current_row As Integer
    Private Sub frmPhieuMuon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SetCbBox(cbomathe, "MaDG", "MaDG", clsDocGia.DSDocGia)
        gbnhapchitiet.Enabled = False
        gbbchitiet.Enabled = False
        rbmuonsach.Checked = True
        'cbomathe.SelectedIndex = 0
    End Sub

    Private Sub cbomathe_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub

    Private Sub cbomathe_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbomathe.SelectedIndexChanged
        KiemTraMaDG()
    End Sub
    Private Sub KiemTraMaDG()
        'If rbmuonsach.Checked Then
        'chuc nang muon
        Dim dt As New DataTable
        dt=clsDocGia.ThongTinDG(cbomathe.Text)
        If dt.Rows.Count <> 1 Then
            lbloi.Visible = True
            txttendg.Text = ""
        Else
            txttendg.Text = dt.Rows(0)("TenDG")
            lbloi.Visible = False
        End If
        'Else
        'xu ly voi chuc nang tra

        'End If
    End Sub
    Private Sub btntaophieu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntaophieu.Click
        If btntaophieu.Text = "&Tạo phiếu" Then

            If Not (clsPhieuMuon.KiemTraMaPhieu(txtmaphieu.Text) And clsDocGia.ThongTinDG(cbomathe.Text).Rows.Count = 1) Then
                MsgBox("Một trong các thông tin nhập vào không hợp lệ", MsgBoxStyle.Information, "Thông tin")
                Exit Sub
            End If
            If clsDocGia.ConNoSach(cbomathe.Text) Then
                MsgBox("Độc giả này chưa trả hết sách nên không thể mượn tiếp được!", MsgBoxStyle.Information, "Thông tin")
                Exit Sub
            End If
            btntaophieu.Text = "&Lưu phiếu"

        ElseIf btntaophieu.Text = "&Lưu phiếu" Then
            If datChiTietPhieu.RowCount < 1 Then Exit Sub
            clsPhieuMuon.ThemPM(txtmaphieu.Text, cbomathe.SelectedValue, nv.MaNV, Date.Now, txtghichu.Text) ' a tam gan no the nay nhe, vi chua co dang nhapu
            For i As Integer = 0 To datChiTietPhieu.RowCount - 1
                clsPhieuMuon.CTPM_Muon(txtmaphieu.Text, datChiTietPhieu.Rows(i).Cells("IDSach").Value, Convert.ToDateTime(datChiTietPhieu.Rows(i).Cells("NgayHenTra").Value, dateVNFormat))
            Next
            MsgBox("Đã lưu phiếu thành công!")
            datChiTietPhieu.Rows.Clear()
            btntaophieu.Text = "&Tạo phiếu"
            PhucHoiBoxNhap()
        ElseIf btntaophieu.Text = "&Xác nhận" Then
            btnphieupphat.Enabled = True
            pmct = clsPhieuMuon.CTPM(txtmaphieu.Text)

            'datChiTietPhieu.DataSource = pmct
            If (pmct.Rows.Count < 1) Then
                MsgBox("Phiếu mượn này đã được trả hết!", MsgBoxStyle.Information, "Thông tin")
                Exit Sub
            End If
            'thiet lap ngay tra, ngay muon
            datChiTietPhieu.Rows.Clear()
            For i As Integer = 0 To pmct.Rows.Count - 1
                datChiTietPhieu.Rows.Add()
                datChiTietPhieu.Rows(i).Cells("IDSach").Value = pmct(i)("IDSach")
                datChiTietPhieu.Rows(i).Cells("TenSach").Value = pmct(i)("TenSach")
                datChiTietPhieu.Rows(i).Cells("NgayHenTra").Value = Convert.ToDateTime(pmct(i)("NgayHenTra")).ToString("dd/MM/yyyy")
                datChiTietPhieu.Rows(i).Cells("NgayMuon").Value = NgayLap.ToString("dd/MM/yyyy")
                datChiTietPhieu.Rows(i).Cells("NgayTra").Value = Date.Now.ToString("dd/MM/yyyy")
            Next
            btntaophieu.Text = "&Trả sách"
        ElseIf btntaophieu.Text = "&Trả sách" Then
            'trả sách
            For i As Integer = 0 To datChiTietPhieu.RowCount - 1
                clsPhieuMuon.TraSach(txtmaphieu.Text, datChiTietPhieu.Rows(i).Cells("IDSach").Value, Date.Now)
            Next
            MsgBox("Đã trả sách!", MsgBoxStyle.Information, "Thông tin")
            datChiTietPhieu.Rows.Clear()
            btntaophieu.Text = "&Xác nhận"
            btnphieupphat.Enabled = False
        End If
    End Sub


    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        PhucHoiSua()
        txtidsach.Text = ""

        'Dim haituan As New TimeSpan(14, 0, 0)

        'dtngayhentra.Value = Date.Now + haituan 'gia sula 2 tuan sau ngay muon
        ''(mac dinh nha)
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoiSua()
        Dim dt As New DataTable
        dt = clsChiTietSach.ThongTinSach(txtidsach.Text)
        If dt.Rows.Count = 1 Then
            If dt.Rows(0)("TrangThai") = "Chưa được mượn" Then
                For Each r As DataGridViewRow In datChiTietPhieu.Rows
                    If r.Cells("IDSach").Value = txtidsach.Text Then
                        MsgBox("Sách này đã được thêm!", MsgBoxStyle.Information, "Thông tin")
                        Exit Sub
                    End If
                Next
                Dim i As Integer = datChiTietPhieu.Rows.Add
                datChiTietPhieu.Rows(i).Cells("IDSach").Value = txtidsach.Text
                datChiTietPhieu.Rows(i).Cells("TenSach").Value = dt.Rows(0)("TenSach")
                datChiTietPhieu.Rows(i).Cells("NgayMuon").Value = Date.Now.ToString("dd/MM/yyyy")
                datChiTietPhieu.Rows(i).Cells("NgayHenTra").Value = dtngayhentra.Value.ToString("dd/MM/yyyy")
                btnNhapLai_Click(sender, e)
            Else
                MsgBox("Sách này đã được mượn", MsgBoxStyle.Information, "Thông báo")
            End If
        Else
            MsgBox("Không tìm thấy sách có ID này", MsgBoxStyle.Information, "Thông báo")
        End If
    End Sub

    Private Sub txtmaphieu_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmaphieu.Leave
        txtmaphieu_TextChanged(sender, e)
    End Sub

    Private Sub rbmuonsach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbmuonsach.CheckedChanged
        If rbmuonsach.Checked Then
            lbmaphieu.Text = "Mã phiếu đã tồn tại" 'neu la muon sach thi loi se la ma phieu da ton tai
            lbloi.Text = "Mã thẻ không tồn tại"
            btntaophieu.Text = "&Tạo phiếu"
            lbngaytra.Enabled = False
            dtngaytra.Enabled = False
            lbngayhentra.Enabled = True
            dtngayhentra.Enabled = True
            'de ngay hen tra la sau 2 tuan chang han
            dtngayhentra.Value = Date.Now + New TimeSpan(14, 0, 0, 0)
            txtghichu.Enabled = True
            cbomathe.Enabled = True
            btnphieupphat.Enabled=False
        Else
            txtghichu.Enabled = False
            cbomathe.Enabled = False
            lbngayhentra.Enabled = False
            dtngayhentra.Enabled = False
            lbngaytra.Enabled = True
            dtngaytra.Enabled = True
            btntaophieu.Text = "&Xác nhận"
            lbmaphieu.Text = "Mã phiếu không tồn tại" 'neu tra sach ma ko nhap dung ma phieu thi la loi
        End If
        'gio a lam hoan thien chuc nang muon sach da nhe uh
    End Sub
    Private Sub PhucHoiSua()
        btnSua.Text = "&Sửa"
    End Sub
    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "&Sửa" Then
            current_row = datChiTietPhieu.CurrentRow.Index
            txtidsach.Text = datChiTietPhieu.CurrentRow.Cells("IDSach").Value 'a bi tnu' mat rui` T_T
            dtngayhentra.Value = datChiTietPhieu.CurrentRow.Cells("NgayHenTra").Value
            btnSua.Text = "&Lưu"
        Else
            Dim dt As New DataTable
            dt = clsChiTietSach.ThongTinSach(txtidsach.Text)
            If dt.Rows.Count = 1 Then
                If dt.Rows(0)("TrangThai") = "Chưa được mượn" Then
                    For Each r As DataGridViewRow In datChiTietPhieu.Rows
                        If r.Cells("IDSach").Value = txtidsach.Text Then
                            MsgBox("Sách này đã được thêm!", MsgBoxStyle.Information, "Thông tin")
                            Exit Sub
                        End If
                    Next
                    datChiTietPhieu.Rows(current_row).Cells("IDSach").Value = txtidsach.Text
                    datChiTietPhieu.Rows(current_row).Cells("NgayHenTra").Value = dtngayhentra.Value
                    btnNhapLai_Click(sender, e)
                Else
                    MsgBox("Sách này đã được mượn", MsgBoxStyle.Information, "Thông báo")
                End If
            Else
                MsgBox("Không tìm thấy sách có ID này", MsgBoxStyle.Information, "Thông báo")
            End If
        End If
    End Sub
  
    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        PhucHoiSua()
        datChiTietPhieu.Rows.Remove(datChiTietPhieu.CurrentRow)
    End Sub

    Private Sub rbtrasach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtrasach.CheckedChanged

    End Sub


    Private Sub frmPhieuMuon_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        cbomathe.SelectedIndex = 0
    End Sub

    Private Sub btntaophieu_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles btntaophieu.TextChanged
        If btntaophieu.Text = "&Tạo phiếu" Or btntaophieu.Text = "&Xác nhận" Then
            gbbchitiet.Enabled = False
            gbnhapchitiet.Enabled = False
            gbthongtinphieu.Enabled = True
            rbmuonsach.Enabled = True
            rbtrasach.Enabled = True
        ElseIf btntaophieu.Text = "&Lưu phiếu" Or btntaophieu.Text = "&Trả sách" Then
            If btntaophieu.Text = "&Lưu phiếu" Then
                gbnhapchitiet.Enabled = True
                btnThem.Enabled = True
                btnSua.Enabled = True
                btnNhapLai.Enabled = True
            Else
                btnThem.Enabled = False
                btnSua.Enabled = False
                btnNhapLai.Enabled=False
            End If
            gbbchitiet.Enabled = True
            gbthongtinphieu.Enabled = False
            rbmuonsach.Enabled = False
            rbtrasach.Enabled = False
        End If
    End Sub

    Private Sub txtmaphieu_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtmaphieu.TextChanged
        check = "maphieu"
        Timer1.Start()
    End Sub

    Private Sub cbomathe_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbomathe.TextChanged
        check = "mathe"
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        t += 1
        If t = 5 Then
            Timer1.Stop()
            t = 0
            If check = "maphieu" Then
                If txtmaphieu.Text = "" Then
                    lbnhapmaphieu.Visible = True
                Else
                    lbnhapmaphieu.Visible = False
                    maphieuhople = clsPhieuMuon.KiemTraMaPhieu(txtmaphieu.Text)
                    If rbmuonsach.Checked Then
                        lbmaphieu.Visible = Not maphieuhople
                    Else
                        If Not maphieuhople Then
                            Dim ttpm As DataTable
                            ttpm = clsPhieuMuon.ThongTinPM(txtmaphieu.Text)
                            cbomathe.SelectedValue = ttpm.Rows(0)("MaDG")
                            txtghichu.Text = ttpm.Rows(0)("GhiChu")
                            NgayLap = ttpm.Rows(0)("NgayLap")
                        End If
                        lbmaphieu.Visible = maphieuhople
                    End If

                End If
            ElseIf check = "mathe" Then
                If cbomathe.Text = "" Then
                    lbnhapmathe.Visible = True
                Else
                    lbnhapmathe.Visible = False
                    KiemTraMaDG()
                End If
            End If

        End If
    End Sub
    Private Sub PhucHoiBoxNhap()
        txtmaphieu.Text = ""
        cbomathe.Text = ""
        txtghichu.Text=""
    End Sub
    Private Sub btnhuybo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnhuybo.Click
        gbbchitiet.Enabled = True
        datChiTietPhieu.Rows.Clear
        rbmuonsach_CheckedChanged(sender, e)
    End Sub

    Private Sub btnphieupphat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnphieupphat.Click
        Dim f As New FrmPhieuPhat
        f.ThongTinPhieu(txtmaphieu.Text, cbomathe.Text, nv.MaNV)
        f.ShowDialog()
    End Sub
End Class